package lab3.systemA;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class SortFilter extends Filter {

	/**
	 * Portul de intrare.
	 **/
	protected BufferedReader pInput;

	/**
	 * Portul de iesire.
	 **/
	protected BufferedWriter pOutput;

	private List<Student> studenti = new ArrayList<Student>();

	public SortFilter(String sName, BufferedReader pInput, BufferedWriter pOutput) {

		// Executarea constructorului din clasa parinte.
		super(sName);

		// Initializarea porturilor de intrare si iesire.
		this.pInput = pInput;
		this.pOutput = pOutput;
	}

	@Override
	protected boolean ready() throws IOException {
		return this.pInput.ready();
	}

	@Override
	protected void work() throws IOException {
		// Citeste o inregistrare corespunzatoare unui student, de la portul de
		// intrare.
		// Student objStudent = new Student(this.pInput.readLine());

		studenti.add(new Student(this.pInput.readLine(), false));
//		System.out.println("1");		
//		pInput.mark(255);
//		System.out.println("2");
//		String nextLine = this.pInput.readLine();
//		System.out.println("3");
//		if (nextLine.trim().length()==0){
//			System.out.println("b");			
//		}
	//	pInput..notify();//.mark(255);
	//	pInput.reset();
		if (!pInput.ready()) {
			System.out.println("r");
			Student[] sArray = studenti.toArray(new Student[studenti.size()]);
			Arrays.sort(sArray, new Comparator<Student>() {
				public int compare(Student s1, Student s2) {
					return s1.sName.compareTo(s2.sName);
				}
			});
			for (int i = 0; i < sArray.length; i++) {
				this.pOutput.write(sArray[i].studentOutput());
				this.pOutput.newLine();
				this.pOutput.flush();
			}

		}
		/*
		 * if (bBusy == true) { Student[] sArray = studenti.toArray(new
		 * Student[studenti.size()]); Arrays.sort(sArray, new
		 * Comparator<Student>() { public int compare(Student s1, Student s2) {
		 * return s2.sName.compareTo(s1.sName); } });
		 * 
		 * for (int i = 0; i < sArray.length; i++) {
		 * this.pOutput.write(sArray[i].studentOutput());
		 * this.pOutput.newLine(); }
		 * 
		 * this.pOutput.flush();
		 * 
		 * }
		 */
		/*
		 * Student objStudent = new Student(this.pInput.readLine(), false); //
		 * String sd = this.pInput.readLine();
		 * this.pOutput.write(objStudent.studentOutput());
		 * this.pOutput.newLine(); this.pOutput.flush();
		 */
		// Scrie inregistrarea corespunzatoare studentului la unul din porturile
		// de iesire in functie de faptul daca studentul a absolvit cursul dat
		// ca parametru de intrare sau nu l-a absolvit.
		// if (objStudent.hasCompleted(this.iCourse)) {
		// // this.pOutputApproved.write(objStudent.toString());
		// // this.pOutputApproved.newLine();
		// // this.pOutputApproved.flush();
		// } else {
		// // this.pOutputRejected.write(objStudent.studentOutput());
		// // this.pOutputRejected.newLine();
		// // this.pOutputRejected.flush();
		// }
	}

}